/**
 * 实现一些基类
 * 创建者：Carburn Ashroom
 * 2025.7.1
 */

#include "base.h"
#include "chess.h"

namespace Base {

    bool Base_pieces::contains(QChar chess, int count) const
    {
        for (const auto& p : pieces)
            if (p.first==chess and p.second>=count)
                return true;
        return false;
    }

    void Base_pieces::set_chess(const QString& name, int count)
    {
        for (auto& c : name) {
            bool find {};
            for (auto& p : pieces)
                if (p.first == c) {
                    p.second = count;
                    find = true;
                    break;
                }
            if (not find)
                pieces.emplaceBack(c, count);
        }
    }

    QString Base_table::html() const
    {
        QString code;
        QTextStream c_str {&code};
        c_str << pre_code;
        for (auto& c : classes)
            c_str << c.html();
        c_str << end_code;
        return code;
    }

    void Base_table::append(Chess::Message&& message)
    {
        bool find {};
        for (auto& c : classes)
            if (c.match(message)) {
                find = true;
                c.append(std::move(message));
                break;
            }
        if (not find) {
            Chess::Class c {message.type()};
            c.append(std::move(message));
            classes.append(std::move(c));
        }
    }

    void Base_table::append_class(Class_enum type, QList<Chess::Message>&& list)
    {
        if (list.isEmpty())
            return;
        Chess::Class c {type};
        c.append(std::move(list));
        classes.append(std::move(c));
    }

    QString Base_table::read_all(const QString& file_name)
    {
        QFile file {file_name};
        file.open(QIODevice::ReadOnly);
        QTextStream f_str {&file};
        return f_str.readAll();
    }
}
